package com.cortex.pineapple.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.DropBoxManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cortex.pineapple.R;
import com.cortex.pineapple.database.SeasonQuerySearch;
import com.cortex.pineapple.database.VisitLink;
import com.cortex.pineapple.fetch.RequestData.SOURCE_TYPE;
import com.cortex.pineapple.listener.DragDropView;
import com.cortex.pineapple.listener.DragDropView.OnStateChangedListener;
import com.cortex.pineapple.listener.SlidListener;
import com.cortex.pineapple.utilities.MessagesLog;
import com.cortex.pineapple.view.CortexTextView;
import com.cortex.pineapple.view.CortexTextView.TYPE_FACE;


public class ZoomInHistoryAdapter extends ItemListAdapter {

	private boolean isLeftSide;
	
	public ZoomInHistoryAdapter(Context context) {
		super(context);
	}
	public ZoomInHistoryAdapter(Context context, List<Header> seasons) {
		super(context, seasons);
	}
	private static class HeaderHolderView{
		TextView dateSeason;
		TextView dateQuery;
		CortexTextView query;
		ViewGroup containerDateSeason;
	}
	public View getHeaderView(int position,View convertView,ViewGroup parent){
		SeasonQuerySearch season = getHeaderByItem(position);
		HeaderHolderView headerHolder;
		SimpleDateFormat dateFormater = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
		SimpleDateFormat timeFormater = (SimpleDateFormat) SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
		if(convertView == null){
			convertView = inflater.inflate(R.layout.view_item_history_header_regular_zoom, null);

			headerHolder = new HeaderHolderView();

			headerHolder.dateSeason = (TextView) convertView.findViewById(R.id.dateSeason);
			headerHolder.dateQuery = (TextView) convertView.findViewById(R.id.dateQuery);
			headerHolder.query = (CortexTextView) convertView.findViewById(R.id.query);
			headerHolder.containerDateSeason = (ViewGroup) convertView.findViewById(R.id.containerDateSeason);

		}
		else{
			headerHolder = (HeaderHolderView) convertView.getTag();
		}


		headerHolder.dateSeason.setText(dateFormater.format(season.getStartSeason()));
		headerHolder.dateQuery.setText(timeFormater.format(season.getStartSeason()));
		headerHolder.query.setTextWithUnderLine(season.getQuery());

		SeasonQuerySearch currentSeason = getHeaderByItem(position);
		SeasonQuerySearch previousSeason = getPreviousHeader(position);

		Calendar currentCal = Calendar.getInstance();
		Calendar previousCal = Calendar.getInstance();

		currentCal.setTime(currentSeason.getStartSeason());
		previousCal.setTime(previousSeason.getStartSeason());

		if(position == 0){
			headerHolder.containerDateSeason.setVisibility(View.VISIBLE);

		}
		else if(currentCal.get(Calendar.DAY_OF_YEAR) != previousCal.get(Calendar.DAY_OF_YEAR)){
			headerHolder.containerDateSeason.setVisibility(View.VISIBLE);
		}
		else{
			headerHolder.containerDateSeason.setVisibility(View.GONE);
		}

		convertView.setTag(headerHolder);
		return convertView;
	}
	private static class ItemHolderView{
		TextView title;
		TextView date;
		TextView url;
		TextView description;
		ImageView sourceType;
		ImageView pointTime;
		ImageView favicon;
		ImageView selectedText;
		int position;
	}
	public View getItemView(final int position,View convertView,ViewGroup parent){
		VisitLink visitLink = (VisitLink) getItem(position);
		ItemHolderView rowHolder;
		SimpleDateFormat timeFormater = (SimpleDateFormat) SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
		if(convertView == null){
			rowHolder = new ItemHolderView();
			convertView = createVisitLink(position,visitLink,(ViewGroup) convertView,rowHolder);
		}
		else{
			rowHolder = (ItemHolderView) convertView.getTag();
		}

		if(position != rowHolder.position){
			convertView = createVisitLink(position,visitLink,(ViewGroup) convertView,rowHolder);
		}
		rowHolder.date.setText(timeFormater.format(visitLink.getStartVisit()));
		rowHolder.title.setText(visitLink.getTitle());
		rowHolder.url.setText(visitLink.getUrl());
		rowHolder.description.setText(visitLink.getDescription());
		rowHolder.sourceType.setImageDrawable(getImageBySource(visitLink.getSource()));
		rowHolder.pointTime.setImageDrawable(visitLink.isFavorite()?
				context.getResources().getDrawable(R.drawable.favorite_visit_link):
				context.getResources().getDrawable(R.drawable.point_time));
		rowHolder.favicon.setImageBitmap(visitLink.getFavicon());
		rowHolder.position = position;
		rowHolder.selectedText.setVisibility(visitLink.getSectionsSelected().isEmpty()?View.GONE:View.VISIBLE);
		convertView.setTag(rowHolder);
		isLeftSide = !isLeftSide;
		return convertView;
	}

	private View createVisitLink(int position,final VisitLink visitLink,ViewGroup convertView,ItemHolderView rowHolder){
		if(position % 2 == 0){
			convertView =  (ViewGroup) inflater.inflate(R.layout.view_history_item_zoom_in_left, null);
		}
		else{
			convertView = (ViewGroup) inflater.inflate(R.layout.view_history_item_zoom_in_right, null);
		}
		rowHolder.date = (TextView) convertView.findViewById(R.id.date);
		rowHolder.title = (TextView) convertView.findViewById(R.id.title);
		rowHolder.url = (CortexTextView) convertView.findViewById(R.id.url);
		rowHolder.description = (CortexTextView) convertView.findViewById(R.id.description);
		rowHolder.sourceType = (ImageView) convertView.findViewById(R.id.sourceType);
		rowHolder.pointTime = (ImageView) convertView.findViewById(R.id.pointTime);
		rowHolder.favicon = (ImageView) convertView.findViewById(R.id.favicon);
		rowHolder.selectedText = (ImageView) convertView.findViewById(R.id.selectedText);

		
		
		
		// drag the url into browser
//		DragDropView drag = new DragDropView(rowHolder.url);
//		drag.setOnStateChangedListener(new OnStateChangedListener() {
//			VisitLink link = visitLink;
//			@Override
//			public void stopDragging(View dragView, int x, int y) {
//				isDragMode = false;
//				if(onDragRowListener != null){
//					onDragRowListener.stopDragging(dragView, x, y, link);
//				}
//			}
//			
//			@Override
//			public void startDragging(View dragView, int x, int y) {
//				if(onDragRowListener != null){
//					onDragRowListener.startDragging(dragView, x, y, link);
//				}
//			}
//			
//			@Override
//			public void moveDragging(View dragView, int x, int y) {
//				isDragMode = true;
//				if(onDragRowListener != null){
//					onDragRowListener.moveDragging(dragView, x, y, link);
//				}
//			}
//		});
		return convertView;
	}
	
	private Drawable getImageBySource(SOURCE_TYPE sourceType){
		Drawable source = null;
		switch(sourceType){
		case IMAGES:
			source = context.getResources().getDrawable(R.drawable.visit_link_images);
			break;
		case NEWS:
			source = context.getResources().getDrawable(R.drawable.visit_link_news);
			break;
		case VIDEO:
			source = context.getResources().getDrawable(R.drawable.visit_link_video);
			break;
		case WEB:
			source = context.getResources().getDrawable(R.drawable.visit_link_web);
			break;
		default:
			break;

		}
		return source;
	}
	public boolean isDragMode(){
		return isDragMode;
	}

	public interface OnDragVisitLinkListener{
		void startDragging(View dragView,int x,int y,VisitLink visitLink);
		void moveDragging(View dragView,int x,int y,VisitLink visitLink);
		void stopDragging(View dragView,int x,int y,VisitLink visitLink);
	}
}
